<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Controller;

use App\Constants\MediaConstant;
use App\Repository\MediaRepository;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\Filesystem\FilesystemFactory;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Psr\Http\Message\ResponseInterface as Psr7ResponseInterface;
use League\Flysystem\Filesystem;

/**
 * Class UploadController
 * @package App\Controller
 * @Controller
 */
class UploadController extends AbstractController
{
    /**
     * @var ResponseInterface
     */
    protected $response;

    public $base_url;

    /**
     * @Inject
     * @var Filesystem
     */
    protected $filesystem;


    public function __construct()
    {
        if (env('OSS_BUCKET') && env('OSS_ENDPOINT')){
            $this->base_url = 'https://image.51job.top';
        }else{
            $this->base_url = 'http://'.env('IMAGE_SERVER_HOST', '127.0.0.1').':' . env('IMAGE_SERVER_PORT', '5504');
        }
    }

    /**
     * Notes: 单张图片上传
     * Author: Bruce.z
     * DateTime: 2021/12/30 17:46
     *
     * @PostMapping(path="image")
     *
     * @return Psr7ResponseInterface
     * @throws \League\Flysystem\FilesystemException
     */
    public function image() : Psr7ResponseInterface
    {
        $file = $this->request->file('image');
        $extension = strtolower($file->getExtension()) ?: 'png';
        $name = $file->getClientFilename();
        //$file_path = BASE_PATH .'/static/upload/'.date('Y') . '/' . date('md') . '/';
        $file_path = '/upload/'.date('Y') . '/' . date('md') . '/';
        $new_file_name = self::randomStr(18) . '.' . $extension;

        $stream = fopen($file->getRealPath(), 'r+');
        $this->filesystem->writeStream($file_path. $new_file_name, $stream);
        fclose($stream);

        //$url = self::BASE_URL . '/static/upload/'.date('Y') . '/' . date('md') . '/' . $new_file_name;
        $url = $this->base_url . '/upload/'.date('Y') . '/' . date('md') . '/' . $new_file_name;

        $data = [
            'catalog_id'=>0,
            'name'=>$name,
            'url'=>$url,
            'type'=>MediaConstant::MEDIA_TYPE_IMG,
        ];
        MediaRepository::getInstance()->baseAdd($data);

        $return = [
            'code'=>MediaConstant::SUCCESS_CODE,
            'msg'=>'成功',
            'data'=>[
                'url'=>$url,
                'name'=>$name
            ]
        ];
        return $this->response->json($return);
    }

    /**
     * Notes: 多张图片上传
     * Author: Bruce.z
     * DateTime: 2021/12/30 17:46
     *
     * @PostMapping(path="imageMany")
     *
     * @return Psr7ResponseInterface
     * @throws \League\Flysystem\FilesystemException
     */
    public function imageMany(): Psr7ResponseInterface
    {
        $file = $this->request->file('imageName');
        dd($file);
        $extension = strtolower($file->getExtension()) ?: 'png';
        $name = $file->getFilename();
        $file_path = BASE_PATH .'/static/upload/'.date('Y') . '/' . date('md') . '/';
        $new_file_name = self::randomStr(18) . '.' . $extension;

        $stream = fopen($file->getRealPath(), 'r+');
        $this->filesystem->writeStream($file_path. $new_file_name, $stream);
        fclose($stream);

        $url = $this->base_url . '/static/upload/'.date('Y') . '/' . date('md') . '/' . $new_file_name;

        $data = [
            'catalog_id'=>0,
            'name'=>$name,
            'url'=>$url,
            'type'=>MediaConstant::MEDIA_TYPE_IMG,
        ];
        MediaRepository::getInstance()->baseAdd($data);

        $return = [
            'code'=>MediaConstant::SUCCESS_CODE,
            'msg'=>'成功',
            'data'=>[
                'url'=>$url
            ]
        ];
        return $this->response->json($return);
    }

    /**
     * Notes: 随机名字
     * Author: Bruce.z
     * DateTime: 2021/12/30 17:48
     * @param $length
     * @return string
     */
    public static function randomStr($length)
    {
        $pattern = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ';
        $key = '';
        for($i=0;$i<$length;$i++) {
            $key .= $pattern[mt_rand(0,35)];
        }
        return $key;
    }
}
